package com.java.tutorials.spring.CallableStatementCreator;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
/**
 * Created by xuzhike on 2016/3/13.
 * http://www.java2s.com/Code/Java/Spring/ImplementsCallableStatementCreator.htm
 * Implements CallableStatementCreator
 */
class Main {
    public static void main(String args[]) throws Exception {
        ApplicationContext ac = new ClassPathXmlApplicationContext("context.xml", Main.class);
        DataSource dataSource = (DataSource) ac.getBean("dataSource");
        // DataSource mysqlDataSource = (DataSource) ac.getBean("mysqlDataSource");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
        jdbcTemplate.call(new MyCallableStatementCreator(), null);

    }
}
class MyCallableStatementCreator implements CallableStatementCreator {
    public CallableStatement createCallableStatement(Connection connection) throws SQLException {
        CallableStatement cs = connection.prepareCall("{? = call f_calculate}");
        cs.registerOutParameter(1, Types.INTEGER);
        return cs;
    }
}
